package tetris.game;

import java.awt.Color;
import java.awt.Graphics;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Tetrad {
	public static final int START_X = 3;
	public static final int START_Y = 0;
	int m_X, m_Y;
	int m_Width;
	int m_Height;
	int m_Color;
	byte [][] m_Map;

	/**
	 * Used to make a tetrad become a copy of another tetrad
	 * @param a_Piece the piece that is to be duplicated
	 */
	public void Copy(Tetrad a_Piece)
	{
		m_X = START_X;
		m_Y = START_Y;
		m_Width = a_Piece.m_Width;
		m_Height = a_Piece.m_Height;
		ClearMap();
		m_Color = a_Piece.m_Color;
		for(int y = 0; y < m_Height; ++y)
		{
			for(int x = 0; x < m_Width; ++x)
			{
				m_Map[y][x] = a_Piece.m_Map[y][x];
			}
		}
	}

/**
 * Constructor, sets values to zero
 */
	Tetrad()
	{
		m_Width = 0;
		m_Height = 0;
		m_Color = 0;
		m_X = 0;
		m_Y = 0;
	}

/**
 * A function used to load from a file and set the Tetrad (Used for the Untouchables)
 * @param PieceNumber the number of the piece you wish to initialize to from the file
 * @return True if successful, False if the load failed or a bad number was passed in 
 */
	public boolean CreatePiece(int PieceNumber)
	{
		
		m_X=START_X; m_Y=START_Y;
		Scanner  inputStream = null;
		switch(PieceNumber)
		{
		case 0:
			try {
				inputStream = new Scanner(new File("BLOCKO.txt"));
			} catch (FileNotFoundException e) {
				e.printStackTrace();
				return false;
			}	break;
		case 1:
			try {
				inputStream = new Scanner(new File("BLOCKI.txt"));
			} catch (FileNotFoundException e) {
				e.printStackTrace();
				return false;
			}	break;
		case 2:
			try {
				inputStream = new Scanner(new File("BLOCKT.txt"));
			} catch (FileNotFoundException e) {
				e.printStackTrace();
				return false;
			}	break;
		case 3:
			try {
				inputStream = new Scanner(new File("BLOCKS.txt"));
			} catch (FileNotFoundException e) {
				e.printStackTrace();
				return false;
			}	break;
		case 4:
			try {
				inputStream = new Scanner(new File("BLOCKZ.txt"));
			} catch (FileNotFoundException e) {
				e.printStackTrace();
				return false;
			}	break;
		case 5:
			try {
				inputStream = new Scanner(new File("BLOCKL.txt"));
			} catch (FileNotFoundException e) {
				e.printStackTrace();
				return false;
			}	break;
		case 6:
			try {
				inputStream = new Scanner(new File("BLOCKJ.txt"));
			} catch (FileNotFoundException e) {
				e.printStackTrace();
				return false;
			}	break;
		default:
			return false;
		}

		m_Width = inputStream.nextInt();
		m_Height = inputStream.nextInt();
		m_Color = inputStream.nextInt();

		m_Map = null;
		m_Map = new byte [4][4];
		
		for(int y = 0; y < m_Height; ++y)
		{
			for(int x = 0; x < m_Width; ++x)
			{
				if(inputStream.hasNextByte())
				{
					byte input = inputStream.nextByte();
					m_Map[y][x] = input;
				}
			}
		}
		
		inputStream.close();
		
		return true;
	}
	
	/**
	 * Fills the m_Map with Zeros
	 */
	private void ClearMap()
	{
		for(int y = 0; y < 4; ++y)
		{
			for(int x = 0; x < 4; ++x)
			{
				m_Map[y][x] = 0;
			}
		}
	}
	
	/**
	 * Rotates the Tetrad Counter Clockwise<br>
	 * Does not check for collision
	 */
	public void RotateCCW()
	{
		byte [][] m_RotatedMap = new byte [4][4];

		for(int x = 0; x < m_Width; ++x)
		{
			for(int y = 0; y < m_Height; ++y)
			{
				if(m_Map[y][x]==2)
				{
					m_X += (x - y);
					m_Y += (y - (m_Width-1-x));
				}
				m_RotatedMap[x][y] = m_Map[y][(m_Width-1)-x];
			}
		}

		int temp = m_Height;
		m_Height = m_Width;
		m_Width = temp;

		m_Map = null;
		m_Map = m_RotatedMap;
	}
	
	/**
	 * Rotates the tetrad Clockwise<br>
	 * Does not check for collision
	 */
	public void RotateCW()
	{
		byte [][] m_RotatedMap = new byte [4][4];

		for(int x = 0; x < m_Width; ++x)
		{
			for(int y = 0; y < m_Height; ++y)
			{
				if(m_Map[y][x]==2)
				{
					m_X += (x - (m_Height-1-y));
					m_Y += (y - x);
				}
				m_RotatedMap[x][y] = m_Map[(m_Height-1)-y][x];
			}
		}

		int temp = m_Height;
		m_Height = m_Width;
		m_Width = temp;

		m_Map = null;
		m_Map = m_RotatedMap;
	}
	
	/**
	 * Used to draw this Tetrad
	 */
	public void Draw(Graphics g)
	{
		for(int y = 0; y < m_Height; y++)
		{
			for(int x = 0; x < m_Width; x++)
			{
				if(m_Map[y][x] != 0)
				{
						switch(m_Color)
						{
						case 1:
							g.setColor(Color.red);		break;
						case 2:
							g.setColor(Color.blue);		break;
						case 3:
							g.setColor(Color.green);	break;
						case 4:
							g.setColor(Color.orange);	break;
						case 5:
							g.setColor(Color.white);	break;
						case 6:
							g.setColor(Color.cyan);		break;
						case 7:
							g.setColor(Color.pink);		break;
							default:
								g.setColor(Color.gray);
						}
					g.fillRect(m_X*32+(x*32), m_Y*32+(y*32), 32, 32);
				}
			}
		}
	}
}
